<!-- 司机资质审查 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @selection-change="selectionChange"
    >
      <template slot="menuLeft">
        <el-button type="primary" size="small" @click="batchPass"
          >批量通过</el-button
        >
      </template>
      <template slot-scope="{ row, index }" slot="menu">
        <el-button
          type="text"
          size="mini"
          icon="el-icon-edit"
          @click.stop="rowEdit(row, index)"
          >审核</el-button
        >
      </template>
      <template slot="formTitle"><span>标题</span></template>
      <template slot="menuForm">
        <el-button type="primary" size="small" @click="approved()"
          >审核通过</el-button
        >
        <el-button type="danger" size="small" @click="fail()">不通过</el-button>
        <el-button plain size="small" @click="$refs.crud.closeDialog()"
          >关闭</el-button
        >
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getQualificationReviewList,
  driverCheck,
  getQualificationReviewCarrierList,
} from "@/api/basedata/driver";

export default {
  data() {
    return {
      form: {
        dynamic: [],
      },
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        addBtn: false,
        editBtn: false,
        editTitle: "主信息",
        delBtn: false,
        updateBtn: false,
        cancelBtn: false,
        selection: true,
        labelWidth: "180",
        menuWidth: "250",
        column: [
          {
            label: "姓名",
            prop: "name",
            search: true,
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请输入姓名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "联系电话",
            prop: "phone",
            search: true,
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请输入联系电话",
                trigger: "blur",
              },
            ],
          },
          {
            label: "从业资格证号",
            prop: "certificateWorkNo",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请输入从业资格证号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "驾驶证号",
            prop: "certificateDriverNo",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请输入驾驶证号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "准驾车型",
            prop: "canDriveCarType",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请输入准驾车型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "身份证有效期",
            type: "datetimerange",
            prop: "idCardValidDate",
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请选择身份证有效期",
                trigger: "change",
              },
            ],
          },
          {
            label: "驾驶证有效期",
            type: "datetimerange",
            prop: "certificateDriverValidDate",
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请选择驾驶证有效期",
                trigger: "change",
              },
            ],
          },
          {
            label: "备注",
            prop: "remark",
            hide: true,
            editDisabled: true,
            showColumn: false,
          },
          {
            label: "创建时间",
            type: "datetime",
            prop: "createTime",
            search: true,
            searchRange: true,
            editDisplay: false,
            addDisplay: false,
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
          },
          {
            label: "身份证正面照片上传",
            prop: "idCardPicFront",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请上传身份证正面照片",
                trigger: "change",
              },
            ],
          },
          {
            label: "身份证背面照片上传",
            prop: "idCardPicBack",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请上传身份证背面照片",
                trigger: "change",
              },
            ],
          },
          {
            label: "驾驶证正页上传",
            prop: "certificateDriverPicFront",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请上传驾驶证正页",
                trigger: "change",
              },
            ],
          },
          {
            label: "驾驶证副页上传",
            prop: "certificateDriverPicBack",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请上传驾驶证副页",
                trigger: "change",
              },
            ],
          },
          {
            label: "从业资格证上传",
            prop: "certificateWorkPic",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "请上传从业资格证",
                trigger: "change",
              },
            ],
          },
          {
            label: "关联承运商",
            type: "dynamic",
            prop: "dynamic",
            hide: true,
            showColumn: false,
            editDisabled: true,
            span: 24,
            children: {
              addBtn: false,
              column: [
                {
                  label: "名称",
                  prop: "name",
                },
                {
                  label: "电话",
                  prop: "phone",
                },
              ],
            },
          },
        ],
      },
      data: [],
      selectionList: [], //选择数据
    };
  },
  components: {},
  computed: {},
  methods: {
    rowEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
      this.form = {
        ...row,
        idCardValidDate: [row.idCardValidStart, row.idCardValidEnd],
        certificateDriverValidDate: [
          row.certificateDriverValidStart,
          row.certificateDriverValidEnd,
        ],
        dynamic: [],
      };
      getQualificationReviewCarrierList({ id: row.id }).then((res) => {
        this.form.dynamic = res.data.data;
      });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let query = Object.assign(params, this.query);
      if (query.createTime) {
        query = {
          ...query,
          start: query.createTime ? query.createTime[0] : "",
          end: query.createTime ? query.createTime[1] : "",
        };
        delete query.createTime;
      }
      this.loading = true;
      getQualificationReviewList(page.currentPage, page.pageSize, query).then(
        (res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.$nextTick(() => {
            this.$refs.crud.$refs.table.doLayout();
          });
        }
      );
    },
    //不通过
    fail() {
      this.$prompt("请输入驳回原因", "驳回原因", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\S/,
        inputErrorMessage: "请输入驳回原因",
      })
        .then(({ value }) => {
          this.verify([
            {
              id: this.form.id,
              pass: false,
              reasonForRejection: value,
            },
          ]);
        })
        .catch(() => {});
    },
    //通过
    approved() {
      this.verify([
        {
          id: this.form.id,
          pass: true,
        },
      ]);
    },
    //批量通过
    batchPass() {
      if (this.selectionList.length <= 0) {
        this.$message({
          message: "请选择要审核的信息",
          type: "warning",
        });
        return false;
      }
      let arr = this.selectionList.map((item) => {
        return {
          id: item.id,
          pass: true,
        };
      });
      this.verify(arr);
    },
    //审核
    verify(arr) {
      driverCheck(arr).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.closeDialog();
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    //多选
    selectionChange(list) {
      this.selectionList = list;
    },
  },
};
</script>

<style></style>
